<template>
    <el-card>
        <el-card>
            <el-row class="form-row">
                <el-form :model="filters" label-position="right" inline ref="formInline">
                    <el-form-item label="小区名称">
                        <div class="filters-input">
                            <el-select v-model="filters.housingId" filterable default-first-option placeholder="请选择小区"
                                       :clearable="true" no-data-text="暂无数据">
                                <el-option v-for="item in housingList" :key="item.id" :label="item.name"
                                           :value="item.id">
                                </el-option>
                            </el-select>
                        </div>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="ios-search" @click="handleQuery('formInline')">搜索</el-button>
                    </el-form-item>
                </el-form>
            </el-row>
            <el-row class="form-row">
                <el-button type="primary" @click="addTerminal()">新增</el-button>
            </el-row>
            <el-table stripe border :loading="tableLoading" :data="tableData" ref="table" style="width: 100%">
                <template slot="empty">暂无数据</template>
                <el-table-column label="小区名称" prop="housingName"></el-table-column>
                <el-table-column label="终端号" prop="terminalNo"></el-table-column>
                <el-table-column label="操作" width="150" align="center">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" @click="addTerminal(scope.row)">修改
                        </el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <Page style="margin: 10px 0;" :total="pageTotal" :current="pageNum" :page-size="pageSize" show-total
                  id="lyPage"
                  show-sizer show-elevator @on-change="handlePage" @on-page-size-change='handlePageSize'>
            </Page>
        </el-card>

        <el-dialog :close-on-click-modal="false" title="配置终端" :visible.sync="terminalConfig" width="900">
            <div class="pp-info">
                <el-form :model="terminal" :rules="rules" ref="terminalForm" label-width="100px"
                         class="demo-ruleForm">
                    <el-form-item label="小区名称" prop="housingId">
                        <el-select v-model="terminal.housingId" filterable default-first-option placeholder="请选择小区"
                                   :clearable="true" no-data-text="暂无数据" style="width: 100%">
                            <el-option v-for="item in housingList" :key="item.id" :label="item.name"
                                       :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="终端号" prop="terminalNo">
                        <el-input v-model="terminal.terminalNo" placeholder="请输入终端号"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="terminalConfig = false">取 消</el-button>
                <el-button type="primary" @click="sureTerminal('terminalForm')">确 定</el-button>
            </div>
        </el-dialog>
    </el-card>
</template>


<script>
    import baseURL from '_conf/url';
    import qs from "qs";
    import moment from "moment";

    export default {
        data() {
            return {
                //校验规则
                rules: {
                    housingId: [
                        {
                            required: true,
                            message: "请选择小区",
                        }
                    ],
                    terminalNo: [
                        {
                            required: true,
                            message: "请输入终端号",
                        }
                    ],
                },
                //查询条件
                filters: {},
                housingList: [],
                //页码
                pageNum: 1,
                //每页数量
                pageSize: 10,
                //总数
                pageTotal: 0,
                tableData: [],
                //表单提交状态
                tableLoading: false,
                terminalConfig: false,
                terminal: {},
            }
        },
        created: function () {
            this.gethouse();
            this.loadData();
        },
        methods: {
            //加载小区
            gethouse() {
                let url = "/api/housing/getHousingByLogin";
                this.postRequest(url).then(res => {
                    // console.log(res);
                    this.housingList = res;
                });
            },
            //加载数据
            loadData: function () {
                if (this.tableLoading) return;
                this.tableLoading = true;
                let params = this.filters;
                params.pageNum = this.pageNum;
                params.pageSize = this.pageSize;
                //与后台交换数据
                this.postRequest("/api/terminal/loadData", params).then(res => {
                    console.log(res)
                    this.tableLoading = false;
                    if (res.code == 20000) {
                        this.tableData = res.data.list;
                        this.pageTotal = res.data.total;
                    } else {
                        this.tableData = [];
                        this.$Message.error({
                            content: res.message,
                            duration: 5
                        });
                    }
                });
            },
            //打开新增修改页面
            addTerminal(row) {
                this.terminal = {};
                this.terminalConfig = true;
                if (row) {
                    this.terminal = row;
                }
            },
            //确定新增修改
            sureTerminal(terminalForm) {
                this.$refs[terminalForm].validate((valid) => {
                    if (valid) {
                        this.postRequest("/api/terminal/saveOrUpdate", this.terminal).then(res => {
                            if (res.code == 20000) {
                                this.terminalConfig = false;
                                this.$Message.success({
                                    content: "操作成功",
                                    duration: 5
                                });
                                this.loadData();
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    }
                });
            },
            handleDelete(id) {
                this.$Modal.confirm({
                    title: "提示",
                    content: "确定删除该条记录？",
                    onOk: () => {
                        this.getRequest("/api/terminal/delTerminal?id=" + id).then(res => {
                            if (res.code == 20000) {
                                this.$Message.success({
                                    content: "操作成功",
                                    duration: 5
                                });
                                this.loadData();
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    }
                });
            },
            //查询
            handleQuery() {
                this.loadData();
            },
            handlePage(value) {
                this.pageNum = value;
                this.loadData();
            },
            handlePageSize(value) {
                this.pageSize = value;
                this.loadData();
            },
        }
    };
</script>
<style lang="less" scoped>
    #pop {
        width: 780px;
        height: 600px;
        display: none;
        position: fixed;
        z-index: 10;
        top: 10%;
        left: 50%;
        margin-left: -390px;
    }
</style>
